<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>第一周作业开灯关灯按钮</title>
    <style>
        body {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100vh;
            margin: 0;
            font-family: Arial, sans-serif;
        }
        #lightImage {
            width: 200px;
            height: auto;
            margin-bottom: 20px;
        }
        button {
            padding: 10px 20px;
            font-size: 16px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <h1>点击按钮切换灯的状态</h1>
    <img id="lightImage" src="off.png" alt="灯的状态">
    <button id="toggleButton">开灯</button>

    <script>
        // 获取按钮和图片元素
        const button = document.getElementById('toggleButton');
        const lightImage = document.getElementById('lightImage');

        // 定义图片路径
        const onImagePath = 'img/on.jpg'; // 开灯图片路径
        const offImagePath = 'img/off.jpg'; // 关灯图片路径

        // 初始状态
        let isLightOn = false;

        // 添加按钮点击事件
        button.addEventListener('click', () => {
            isLightOn = !isLightOn; // 切换状态
            if (isLightOn) {
                // 开灯状态
                lightImage.src = onImagePath;
                button.textContent = '关灯';
            } else {
                // 关灯状态
                lightImage.src = offImagePath;
                button.textContent = '开灯';
            }
        });
    </script>
</body>
</html>